#include"Algorithm.h" 
#include"Action.h"
#include"InputWidget.h"

/*!
\file Algorithm.cpp
\brief Contains implementation of Algorithm class.
\ingroup Algorithm
*/

Algorithm::Algorithm( Graph *graph ){
	m_graph = graph;
	connect( m_graph, SIGNAL( reportAction(Action*) ), this, SLOT(_reportAction(Action*)) );
}

void Algorithm::containerAdd(){
	m_containers.push_back( Container() );
	connect( &m_containers[m_containers.size()-1], SIGNAL(reportAction(ContainerElementAction*)), this, SLOT(_reportAction(ContainerElementAction*)) );
}

void Algorithm::containerPop(){
	if( m_containers.size() > 0 ){
		_reportAction( new ContainerRemoveAction(m_containers.size()-1) );
		disconnect( &m_containers[m_containers.size()-1] );
		m_containers.pop_back();
	}
}

int Algorithm::containerCount()const{
	return m_containers.size();
}

Container &Algorithm::container( int i ){
	return m_containers[i];
}

void Algorithm::_reportAction( Action *action ){
	m_actions.push_back( action );
}

void Algorithm::_reportAction( ContainerElementAction *action ){
	int i = 0;
	QObject *s = sender();
	while( i < m_containers.size() && s != &m_containers[i] )
		i++;
	action->setContainer(i);
	_reportAction( (Action*) action );
}

Action *Algorithm::_popAction(){
	if( m_actions.size() > 0 ){
		Action *ret = m_actions[0];
		m_actions.pop_front();
		return ret;
	}else
		return 0;
}

void Algorithm::highlight( const Node &node ){
	_reportAction( new NodeHighlightAction( node.identifier() ) );
}

void Algorithm::highlight( const Edge &edge ){
	_reportAction( new EdgeHighlightAction( edge.identifier() ) );
}

void Algorithm::comment( QString c ){
	_reportAction( new CommentAction(c) );
}

Node *Algorithm::getNode( QString comment ){
	return InputWidget::getNode(graph(), comment);
}

Edge *Algorithm::getEdge( QString comment ){
	return InputWidget::getEdge(graph(), comment);
}

